//---------------------------------------------------------------------->
//
// Copyright 2002 e Professional Corporation.  All Rights Reserved.
//
// File:         LoginServlet.java
//
// Author:   Manz
//
// Date:		15/08/2002
//
//---------------------------------------------------------------------->

package com.epro.standard.frontend.servlet;

import java.io.OutputStream;
import java.io.PrintWriter;

import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class EproServlet extends HttpServlet implements java.io.Serializable {

    protected com.epro.standard.frontend.system.MessageDB messageDB;

    public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException {
        messageDB = 
                new com.epro.standard.frontend.system.MessageDB(config.getServletContext());
        super.init(config);
    }

    /*
		Default doGet method.
		doGet must reject all unauthorize requests.
	*/

    public void doGet(javax.servlet.http.HttpServletRequest request, 
                      javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, 
                                                                              java.io.IOException {

        response.setContentType("text/html");

        String authorization = request.getHeader("Authorization");

        if (authorization == null) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.setHeader("WWW-Authenticate", 
                               "BASIC realm=\"privileged-few\"");
        } else {
            response.sendRedirect("/Hutchison/unAuthorize.jsp");
        }
    }

    private String decodeUserInfo(String userInfo) throws java.io.IOException {

        userInfo = userInfo.substring(6).trim(); // Clear "Basic "
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

        return new String(decoder.decodeBuffer(userInfo));
    }

    /*
		response.setContentType("text/html") ;
		
		String encoding = request.getHeader("Accept-Encoding") ;
		String useGzip = request.getParameter("userGzip") ;
System.out.println("Found encoding : " + encoding);
		
		PrintWriter out ;
		String title ;
		if(encoding != null && encoding.indexOf("gzip") != -1 && !"none".equals(useGzip)) {
System.out.println("Use GZIP.");
			title = "Hello GZip" ;
			OutputStream out1 = response.getOutputStream() ;
			out = new PrintWriter(new GZIPOutputStream(out1), false) ;
			response.setHeader("Content-Encoding", "GZIP") ;
		} else {
System.out.println("Not use GZIP.");
			title = "Hello World" ;
			out = response.getWriter() ;
		}
		
		out.close() ;

	 * 	 * */

    protected void drawPage(HttpServletRequest request, 
                            HttpServletResponse response, 
                            String srcPage) throws javax.servlet.ServletException, 
                                                   java.io.IOException {
        String encoding = request.getHeader("Accept-Encoding");
        String useGzip = request.getParameter("userGzip");
        PrintWriter out;
        String title;
        if (encoding != null && encoding.indexOf("gzip") != -1 && 
            !"none".equals(useGzip)) {
            //				System.out.println("Use GZIP.");
            title = "Hello GZip";
            OutputStream out1 = response.getOutputStream();
            out = new PrintWriter(new GZIPOutputStream(out1), false);
            response.setHeader("Content-Encoding", "GZIP");
        } else {
            //				System.out.println("Not use GZIP.");
            title = "Hello World";
            out = response.getWriter();
        }
        out.println(srcPage);
        out.close();
    }

    /*
	 * 	Deplecated.
	 * 	Replace with drawPage(request, response, srcPage) instead.
	 * */

    protected void drawPage(javax.servlet.http.HttpServletResponse response, 
                            String srcPage) throws javax.servlet.ServletException, 
                                                   java.io.IOException {

        response.setContentType("text/html; charset=WINDOWS-874");
        java.io.PrintWriter out = 
            new java.io.PrintWriter(new java.io.OutputStreamWriter(response.getOutputStream()));
        out.println(srcPage);
        out.close();
    }
}

